using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Azad.BCL.CustomGrid
{
    [SupportsEventValidation]
    internal class DataControlLinkButton : LinkButton
    {
        // Fields
        private string _callbackArgument;
        private IPostBackContainer _container;
        private bool _enableCallback;

        /// <summary>
        /// Initializes a new instance of the <see cref="DataControlLinkButton"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        internal DataControlLinkButton(IPostBackContainer container)
        {
            _container = container;
        }

        /// <summary>
        /// Enables the callback.
        /// </summary>
        /// <param name="argument">The argument.</param>
        internal void EnableCallback(string argument)
        {
            _enableCallback = true;
            _callbackArgument = argument;
        }

        /// <summary>
        /// Creates a <see cref="T:System.Web.UI.PostBackOptions"/> object that represents the <see cref="T:System.Web.UI.WebControls.LinkButton"/> control's postback behavior.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Web.UI.PostBackOptions"/> that represents the <see cref="T:System.Web.UI.WebControls.LinkButton"/> control's postback behavior.
        /// </returns>
        protected override PostBackOptions GetPostBackOptions()
        {
            if (_container != null)
            {
                return _container.GetPostBackOptions(this);
            }
            return base.GetPostBackOptions();
        }

        /// <summary>
        /// Renders the control to the specified HTML writer.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            SetCallbackProperties();
            base.Render(writer);
        }

        /// <summary>
        /// Sets the callback properties.
        /// </summary>
        private void SetCallbackProperties()
        {
            if (_enableCallback)
            {
                ICallbackContainer container = _container as ICallbackContainer;
                if (container != null)
                {
                    string callbackScript = container.GetCallbackScript(this, _callbackArgument);
                    if (!string.IsNullOrEmpty(callbackScript))
                    {
                        OnClientClick = callbackScript;
                    }
                }
            }
        }
        
        /// <summary>
        /// Gets or sets a value indicating whether validation is performed when the <see cref="T:System.Web.UI.WebControls.LinkButton"/> control is clicked.
        /// </summary>
        /// <value></value>
        /// <returns>true if validation is performed when the <see cref="T:System.Web.UI.WebControls.LinkButton"/> control is clicked; otherwise, false. The default value is true.
        /// </returns>
        public override bool CausesValidation
        {
            get
            {
                if (_container != null)
                {
                    return false;
                }
                return base.CausesValidation;
            }
            set
            {
                if (_container != null)
                {
                    throw new NotSupportedException("CannotSetValidationOnDataControlButtons");
                }
                base.CausesValidation = value;
            }
        }
    }
}
